<?php
function cat_list($category ,$count,$cat_img,$len_content,$len_title){
?>

      <?php query_posts("showposts=$count&cat=$category")?>
	<div  class="widget widget_nav_menu panel panel-niit panel-primary">
    <div class="arrow"><a href="<?php echo get_category_link($category);?>">
      <span class="dashicons dashicons-portfolio"></span></a>
    </div>
    <div class="panel-heading widgettitle">
      <a class="panel-header" href="<?php echo get_category_link($category);?>">
        <?php single_cat_title();  ?>
      </a>
    </div>

        <ul class="menu">
          <?php
          $i = 0;
          while (have_posts()) : the_post();

            if( $len_content > 0 and $i == 0) {
              ?>
              
              <img src="<?php if ($cat_img == "") {bloginfo('template_url'); ?>/images/cat5.jpg"<?php }else{echo $cat_img.'"';} ?> alt="" width="108px" height="80px">
              <div class="picat">
                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">

            <?php
             echo mb_strimwidth(get_the_title(),0,$len_title-4,"...");
            ?></a><br />

              <?php ///显示字数限制的文章标题结束 ?>

              <?php

              echo mb_strimwidth(strip_tags(apply_filters('the_content', get_the_content())), 0, $len_content,"...");

              ?>
              <a target="_blank" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" class="orange">[详细]</a>
              </div>
              <hr />
              <?php

            }
            else {
              ?>
              <li class="menu-item menu-item-type-custom menu-item-object-custom ">
                <?php
                $post_id = get_the_ID();
                    
                $link_url=get_post_meta($post_id, 'link_url', $single = true);
                if (  !$link_url ) {
                  //echo '<a target=_blank href="'.$link_url .'" title="';
                  //the_title_attribute();
                  //echo '">';
                  $link_url = get_the_permalink();
                  //echo $link_url;
                }
                
                ?>
                <a href="<?php echo $link_url;?>" title="<?php the_title_attribute();?>">

              <?php
                echo mb_strimwidth(get_the_title(),0,$len_title,"...");
//the_time('Y-m-d') ?>
              </a></li>
              <?php
            }
            $i = $i+1;
          endwhile; ?>
          </ul>
        
      </div>
<?php
}
//得到缩略图
function catch_first_image($content) {
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
  $first_img = @$matches [1] [0];

  if(empty($first_img)){ //Defines a default image
          return '';
  }
  return $first_img;
 }
function search_tab($opac_uri,$duxiu_uri,$yidu_uri){
  ?>
  <!-- Nav tabs -->
  <div class="bs-docs-example">
    

<ul class="nav nav-tabs" id="search_tab">
  <?php if( !empty($opac_uri) ) { ?>
  <li class="active"><a href="#OPAC" data-toggle="tab" >图书馆OPAC查询</a></li>
  <?php }?>
  <?php if( !empty($duxiu_uri) ) { ?>
  <li><a href="#duxiu" data-toggle="tab">读秀一站式检索</a></li>
  <?php }?>
  <?php if( !empty($yidu_uri) ) { ?>
  <li><a href="#yidu" data-toggle="tab">Calis检索E读</a></li>
  <?php }?>
  
</ul>

<!-- Tab panes -->
<div class="tab-content">

  <?php if( !empty($opac_uri) ) { ?>
  <div class="tab-pane active" id="OPAC">

    <div class="search_bar opac">
     
    <form id="f" name="f" target="blank" method="get" action="http://opac.niit.edu.cn/opac/openlink.php">
      <div class="row-fluid">
      <div class="span12"><div class="input-prepend">
      <select name="strSearchType" >

        <option value="title">题&nbsp;&nbsp;名</option>
        <option value="author">责任者</option>
        <option value="keyword">主题词</option>
        <option value="isbn">ISBN/ISSN</option>
        <option value="asordno">订购号</option>
        <option value="coden">分类号</option>
        <option value="callno">索书号</option>
        <option value="publisher">出版社</option>
        <option value="series">丛书名</option>
        <option value="tpinyin">题名拼音</option>
        <option value="apinyin">责任者拼音</option>
      </select>
     
        
        <input type="text" class="paddingleft input-xlarge"  id="strText" name="strText" >
      
        <button type="submit" value="检索" class="btn btn-primary">检索</button>
        <input type="hidden" name="__hash__" value="3bde5e984485f978fd388358dc02c73b">
      </div>
      </div>
    
    </div>
    </form>
  </div>
</div>

<?php }?>
  <?php if( !empty($duxiu_uri) ) { ?>
  <div class="tab-pane" id="duxiu">
    <div class="search_bar duxiu">
      <!--
      <iframe allowtransparency="true" id="duxiuframe14" border="0" vspace="0" hspace="0" marginwidth="0" marginheight="0" framespacing="0" frameborder="0" scrolling="no" width="750" height="56" src="http://www.duxiu.com.j.niit.edu.cn/pop/isearch.jsp?style=14&amp;sw=输入检索词&amp;logo=&amp;bimg=&amp;swsize=60&amp;enc=utf-8"></iframe>
      -->
    </div>
  </div>
  <?php }?>
  <?php if( !empty($duxiu_uri) ) { ?>
  <div class="tab-pane" id="yidu">
    <div class="search_bar yidu">
      <iframe name="frame4" width="100%" height="150" frameborder="0" scrolling="no" src="http://lib.niit.edu.cn/xd/Public/yidu_edu.html" allowtransparency="true"></iframe>
    </div>
  </div>
  <?php }?>
  
  </div>
</div>
<script>
  jQuery(function () {
    jQuery('#search_tab a:first').tab('show')
  })
</script>

  <?
}
function book_tab2acc($category,$count,$id,$show_desc=false){
  $len_title = 6;
  $len_content = 135;
  query_posts("showposts=$count&cat=$category");
  $i = 0;
  $book_imgs  = array();
  while (have_posts()) : the_post();
    $content = get_the_content();
    $txt =strip_tags($content);
    if (mb_strlen($txt)>$len_content) {
              $txt = mb_substr( $txt, 0, $len_content);
              $txt = $txt."...";
    }

    $image = addslashes(catch_first_image($content) );
    $book_imgs[$i] = array("title"=>get_the_title(),
                "img"=>$image,
                "link"=>get_the_permalink(),
                "txt"=>$txt);
    $i++;            
  endwhile;  
  //var_export($book_imgs);        
  ?>
  <div id="<?php echo 'tab2acc-'.$id;?>">
  <ul class="resp-tabs-list">
    <?php
          $ac = 0;
          foreach ($book_imgs as $key => $book) {
            if($ac == 0 ) {
              $active = "active";
              $ac = 1;
            }else{
              $active="";
            }
            $title = $book['title'];
            if (mb_strlen($title)>$len_title) {
              $title = mb_substr($title, 0, $len_title)."...";
            }
            echo '<li>'.$title.'</li>';
          }    
    ?>
  </ul> 
  <div class="resp-tabs-container">                                                        
    <?php
          $ac = 0;
          foreach ($book_imgs as $key => $book) {
            if($ac == 0 ) {
              $active = "active";
              $ac = 1;
            }else{
              $active="";
            }
            $title = $book['title'];
            if (mb_strlen($title)>$len_title) {
              //$title = mb_substr($title, 0,$len_title);
            }
            $image = '<div class="row-fluid"><div class="span6"><a target="_blank" href="'.$book["link"].'"><img height="250px" src="'.stripcslashes($book["img"]).'"></a></div>';
            
            $content = $book["txt"];

            echo '<div>'.$image.'<div class="span6"><a target="_blank" href="'.$book["link"].'"><h3>'.$title.'</h3></a><p>'.$content.'</p></div></div></div>';
          }    
    ?>
  </div>
</div>
  <script type="text/javascript">
  jQuery('#<?php echo 'tab2acc-'.$id;?>').easyResponsiveTabs({type: 'vertical'});
  </script>
<?php
}

function book_carousel($category,$count,$id,$show_desc=false){
  query_posts("showposts=$count&cat=$category");
  $i = 0;
  $book_imgs  = array();
  while (have_posts()) : the_post();
    $content = get_the_content();
    $txt = mb_substr( strip_tags($content), 0, 300);
    $image = addslashes(catch_first_image($content) );
    $book_imgs[$i] = array("title"=>get_the_title(),
                "img"=>$image,
                "link"=>get_the_permalink(),
                "txt"=>$txt);
    $i++;            
  endwhile;  
  //var_export($book_imgs);        
  ?>
<div id="carousel-book-<?php echo $id;?>" style="height:370px;" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
          <?php
          $ac = 0;
          foreach ($book_imgs as $key => $book) {
            if($ac == 0 ) {
              $active = "active";
              $ac = 1;
            }else{
              $active="";
            }
            echo '<li data-target="#carousel-book-'.$id.'" data-slide-to="'.$key.'" class="'.$active.'"></li>';
          }    
          ?>
        </ol>
        <div class="carousel-inner">
          <?php
          $ac = 0;
          foreach ($book_imgs as $key => $book) {
            if($ac == 0 ) {
              $active = "active";
              $ac++;
            }else{
              $active="";
            }

            ?>
            <div class="item <?php echo $active;?>">
              
                <div class="row-fluid"> 
                  <div class="span4 clearfix">
                    <a href="<?php echo $book['link'];?>">
                    <img class="" 
                    style="margin-top:10px;" 
                    data-src="holder.js/350x350/auto/#777:#555/text:<?php echo $book['title'];?> " 
                    alt="<?php echo $book['title'];?>" 
                    src="<?php echo $book['img'];?>" width="350px" height="350px">
                    </a>
                  </div>
            <?php if ($show_desc == true) { ?>
            <div class="span8 clearfix">
              <div class="">
                <a href="<?php echo $book['link'];?>">
                <h3><?php echo $book['title'];?></h3>
                </a>
                <p><?php echo $book['txt'];?>...<br />
                  <a href="<?php echo $book['link'];?>">(阅读更多)</a></p>
                
              </div>
            </div>
            <?php }; ?>

         
            </div>
          </div>
            <?php 
          }    
          ?>


        </div>
        <a class="left carousel-control" href="#carousel-book-<?php echo $id;?>" data-slide="prev">
          <span class="glyphicon glyphicon-chevron-left"></span>
        </a>
        <a class="right carousel-control" href="#carousel-book-<?php echo $id;?>" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right"></span>
        </a>
      </div>
<?php
}
/////// 带过滤功能的最新文章
//Adds vpn358_recent_posts_widget widget.
class vpn358_recent_posts_widget extends WP_Widget {
        //Register widget with WordPress.
        public function __construct() {
                parent::__construct(
                        'vpn358_recent_posts_widget', // Base ID
                        'vpn358最新文章', // Name
                        array( 'description' => '带分类过滤功能的最新文章', ) // Args
                );
        }
        public function widget( $args, $instance ) {
                extract( $args );
                $title = apply_filters( 'widget_title', $instance['title'] );
                $post_num = apply_filters( 'widget_title', $instance['post_num'] );
                $catID = apply_filters( 'widget_title', $instance['catID'] );

                echo $before_widget;

                if ( ! empty( $title ) ){
                  ?>
                  <div class="arrow">
                    <a href="<?php echo get_category_link($catID);?>">
                    <span class="dashicons dashicons-portfolio"></span></a>
                  </div>
                  <?php
                  echo $before_title.'<a class="panel-header" href="'.get_category_link($catID).'">'.$title.'</a>'. $after_title; 
                }
                      ?>

                <ul >
                        <?php
                        global $post;
                        $tmp_post = $post;
                        $recent_posts = get_posts('orderby=ASC&numberposts='.$post_num.'&category='.$catID);
                        foreach( $recent_posts as $post ) { setup_postdata($post); ?>
                                <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                                <?php
                                                echo mb_strimwidth(get_the_title(),0,38,"...");
                                                ?></a>
                                </li>
                        <?php } $post = $tmp_post; setup_postdata($post); ?>
                </ul>

                <?php
                echo $after_widget;
        }
        public function update( $new_instance, $old_instance ) {
                $instance = array();
                $instance['title'] = strip_tags( $new_instance['title'] );
                $instance['post_num'] = strip_tags( $new_instance['post_num'] );
                $instance['catID'] = strip_tags( $new_instance['catID'] );
                return $instance;
        }

        public function form( $instance ) {
                if ( isset( $instance[ 'title' ] ) ) {
                        $title = $instance[ 'title' ];
                }
                else {
                        $title = 'Recent Posts';
                }
                if ( isset( $instance[ 'post_num' ] ) ) {
                        $post_num = $instance[ 'post_num' ];
                }
                else {
                        $post_num = 5;
                }
                if ( isset( $instance[ 'catID' ] ) ) {
                        $catID = $instance[ 'catID' ];
                }
                else {
                        $catID = '';
                }
                ?>
                <p>
                        <label for="<?php echo $this->get_field_id( 'title' ); ?>">标题:</label>
                        <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
                        <label for="<?php echo $this->get_field_id( 'post_num' ); ?>">显示数量:</label>
                        <input class="widefat" id="<?php echo $this->get_field_id( 'post_num' ); ?>" name="<?php echo $this->get_field_name( 'post_num' ); ?>" type="text" value="<?php echo esc_attr( $post_num ); ?>" />
                        <label for="<?php echo $this->get_field_id( 'slug' ); ?>">选择分类:</label>
                        <?php
                        wp_dropdown_categories(array(
                                'name' => $this->get_field_name( 'catID' ),
                                'hide_empty' => 0,
                                'orderby' => 'name',
                                'show_count' => 1,
                                'selected' => esc_attr( $catID ),
                                'hierarchical' => true,
                                'show_option_none' =>'全部分类',
                                'echo' => 1
                                ));
                                ?>
                </p>
                <?php
        }
} // class zww_recent_posts_widget
// register zww_recent_posts_widget widget
add_action( 'widgets_init', create_function( '', 'register_widget( "vpn358_recent_posts_widget" );' ) );

function vpn358_register_sidebars() {
    register_sidebar(array(
        'id' => 'sidebar1',
        'name' => 'Main Sidebar',
        'description' => 'Used on every page BUT the homepage page template.',
        'before_widget' => '<div id="%1$s" class="widget %2$s panel-niit panel-primary">',
        'after_widget' => '</div>',
        'before_title' => '<div class="panel-heading widgettitle">',
        'after_title' => '</div>',
    ));

    register_sidebar(array(
        'id' => 'sb_homepage',
        'name' => 'Homepage Sidebar',
        'description' => 'Used only on the homepage page template.',
        'before_widget' => '<div id="%1$s" class="widget %2$s panel-niit panel-primary">',
        'after_widget' => '</div>',
        'before_title' => '<h4 class="widgettitle">',
        'after_title' => '</h4>',
    ));

    register_sidebar(array(
      'id' => 'footer1',
      'name' => 'Footer 1',
      'before_widget' => '<div id="%1$s" class="widget span4 %2$s">',
      'after_widget' => '</div>',
      'before_title' => '<h4 class="widgettitle">',
      'after_title' => '</h4>',
    ));
  }
vpn358_register_sidebars();

//前台启用图标字体
//add_action( 'wp_enqueue_scripts', 'vpn358_load_dashicons' );
function vpn358_load_dashicons() {
    //wp_enqueue_style( 'dashicons' );
}



// enqueue styles
if( !function_exists("vpn358_theme_styles") ) {  
    function vpn358_theme_styles() { 
        // This is the compiled css file from LESS - this means you compile the LESS file locally and put it in the appropriate directory if you want to make any changes to the master bootstrap.css.
        wp_register_style( 'Tabs-to-Accordion', get_stylesheet_directory_uri() . '/css/easy-responsive-tabs.css', array(), '1.0', 'all' );
        wp_enqueue_style( 'Tabs-to-Accordion' );

    }
}
add_action( 'wp_enqueue_scripts', 'vpn358_theme_styles' );

// enqueue javascript
if( !function_exists( "vpn358_theme_js" ) ) {  
  function vpn358_theme_js(){
  
    wp_register_script( 'Tabs-to-Accordion', 
      get_stylesheet_directory_uri() . '/js/easyResponsiveTabs.js', 
      array('jquery'), 
      '1.2' );
  
    wp_enqueue_script('Tabs-to-Accordion');
    
  }
}
add_action( 'wp_enqueue_scripts', 'vpn358_theme_js' );

//禁用Open Sans
class Disable_Google_Fonts {
        public function __construct() {
                add_filter( 'gettext_with_context', array( $this, 'disable_open_sans'             ), 888, 4 );
        }
        public function disable_open_sans( $translations, $text, $context, $domain ) {
                if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
                        $translations = 'off';
                }
                return $translations;
        }
}
$disable_google_fonts = new Disable_Google_Fonts;
?>